package com.fjb.controller.product;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.product.CategoryStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.common.plugins.ZTree;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.pojo.product.ProductCategory;
import com.fjb.pojo.product.query.ProductCategoryQuery;
import com.fjb.pojo.user.SysUser;
import com.fjb.service.product.ProductCategoryService;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController extends BaseController{
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	 * @Description:显示分页列表
	 * @param request
	 * @return
	 * ModelAndView
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午5:58:26
	 */
	@RequestMapping(value="/showPageList")
    public ModelAndView showPageList(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/product/product_category_list");	
    	mv.addObject(FILE_SERVICE_LOCATION, GlobalConfig.FILE_SERVICE_LOCATION);
    	return mv;
    }
	
	/**
	 * @Description:分页列表查询
	 * @param request
	 * @param pageInfoQuery
	 * @return
	 * JsonPageResult<List<ProductCategory>>
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午6:07:24
	 */
	@RequestMapping(value = "/selectPageList")
    @ResponseBody
    public JsonTotalResult<List<ProductCategory>> selectPageList(HttpServletRequest request,ProductCategoryQuery categoryQuery,
    		PageQuery pageQuery){
        return productCategoryService.selectPageList(categoryQuery,pageQuery);
    }
	
	/**
	 * @Description:查询产品分类 树形列表
	 * @param request
	 * @return
	 * JsonResult<List<ZTree>>
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午6:31:18
	 */
	@RequestMapping(value = "/selectZTreeList")
    @ResponseBody
    public JsonResult<List<ZTree>> selectZTreeList(HttpServletRequest request){
		QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>();
		List<ProductCategory> infoList = productCategoryService.list(queryWrapper);
		ArrayList<ZTree> zTreeList = new ArrayList<ZTree>();
		if(infoList.size()>0) {
			for (ProductCategory productCategory : infoList) {
				Integer id = productCategory.getId();
				Integer parentId = productCategory.getParentId();
				String categoryName = productCategory.getCategoryName();
				ZTree zTree = new ZTree();
				zTree.setId(id);
				zTree.setpId(parentId);
				zTree.setName(categoryName);
				zTreeList.add(zTree);
			}
		}
		return new JsonResult<List<ZTree>>(zTreeList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:添加
	 * @param request
	 * @param categoryId
	 * @param brandId
	 * @param sort
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午2:02:37
	 */
	@RequestMapping(value = "/addInfo")
    @ResponseBody
    public JsonResult<Boolean> addInfo(HttpServletRequest request,Integer brandId
    		,Integer sort){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null) {	
			httpCode.setMsg("品牌id为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(sort==null) {
			httpCode.setMsg("排序不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String categoryName = request.getParameter("categoryName");
		if(StringUtils.isBlank(categoryName)) {
			httpCode.setMsg("分类名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String keywords = request.getParameter("keywords");
		if(StringUtils.isBlank(keywords)) {
			httpCode.setMsg("类目关键字不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String initial = request.getParameter("initial");
		if(StringUtils.isBlank(initial)) {
			httpCode.setMsg("首字母不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String description = request.getParameter("description");
		if(StringUtils.isBlank(description)) {
			httpCode.setMsg("说明不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
			
		ProductCategory category = new ProductCategory();
		category.setParentId(0);
		category.setParentIds("0");
		category.setMainUserId(mainUserId);
		category.setCategoryName(categoryName);
		category.setKeywords(keywords);
		category.setBrandId(brandId);
		category.setSort(sort);
		category.setInitial(initial);
		category.setCategoryLevel(1);
		category.setDescription(description);
		category.setCreateTime(LocalDateTime.now());
		category.setCreateUserId(userId);	
		category.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		boolean b = productCategoryService.save(category);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改
	 * @param request
	 * @param categoryId
	 * @param brandId
	 * @param sort
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午2:02:25
	 */
	@RequestMapping(value = "/updateInfo")
    @ResponseBody
    public JsonResult<Boolean> updateInfo(HttpServletRequest request,Integer categoryId,Integer sort){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(categoryId==null) {
			httpCode.setMsg("类别id不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(sort==null) {	
			httpCode.setMsg("排序不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String categoryName = request.getParameter("categoryName");
		if(StringUtils.isBlank(categoryName)) {
			httpCode.setMsg("分类名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String keywords = request.getParameter("keywords");
		if(StringUtils.isBlank(keywords)) {
			httpCode.setMsg("类目关键字不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String initial = request.getParameter("initial");
		if(StringUtils.isBlank(initial)) {
			httpCode.setMsg("首字母不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String description = request.getParameter("description");
		if(StringUtils.isBlank(description)) {
			httpCode.setMsg("说明不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
		
		QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>();
		queryWrapper.eq("id", categoryId);
		queryWrapper.eq("main_user_id", mainUserId);
		ProductCategory oldCategory = productCategoryService.getOne(queryWrapper);
		if(oldCategory==null) {
			httpCode.setMsg("分类信息不存在");
			return new JsonResult<Boolean>(null, httpCode);
		}
		ProductCategory category = new ProductCategory();
		category.setId(categoryId);
		category.setCategoryName(categoryName);
		category.setKeywords(keywords);
		category.setSort(sort);
		category.setInitial(initial);
		category.setDescription(description);
		category.setUpdateTime(LocalDateTime.now());
		category.setUpdateUserId(userId);
		boolean b = productCategoryService.updateById(category);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:根据id查询
	 * @param request
	 * @param categoryId
	 * @return
	 * JsonResult<ProductCategory>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午1:50:33
	 */
	@RequestMapping(value = "/getById")
    @ResponseBody
    public JsonResult<ProductCategory> getById(HttpServletRequest request,Integer categoryId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(categoryId==null) {	
			return new JsonResult<ProductCategory>(null, httpCode);
		}	
		ProductCategory category = productCategoryService.getById(categoryId);
		return new JsonResult<ProductCategory>(category, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:查询正常的产品类别
	 * @param request
	 * @param brandId
	 * @return
	 * JsonResult<List<ProductCategory>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月18日 下午4:43:46
	 */
	@RequestMapping(value = "/selectNormalList")
    @ResponseBody
    public JsonResult<List<ProductCategory>> selectNormalList(HttpServletRequest request,Integer brandId){
		//HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		/*if(brandId==null) {
			httpCode.setMsg("品牌id不能为空");
			return new JsonResult<List<ProductCategory>>(null, httpCode);
		}*/
		QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>();
		if(brandId!=null) {			
			queryWrapper.eq("brand_id", brandId);
		}
		queryWrapper.eq("category_status", CategoryStatus.STATUS_1.getCode());
		List<ProductCategory> list = productCategoryService.list(queryWrapper);
        return new JsonResult<List<ProductCategory>>(list, HttpCode.SUCCESS);
    }
	
	/**
	 * @Description:修改分类状态
	 * @param request
	 * @param categoryId
	 * @param categoryStatus
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年3月2日 下午8:23:31
	 */
	@RequestMapping(value = "/updateStatus")
    @ResponseBody
    public JsonResult<Boolean> updateStatus(HttpServletRequest request,Integer categoryId,Integer categoryStatus){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(categoryId==null||categoryId==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		ProductCategory pc = new ProductCategory();
		pc.setId(categoryId);
		pc.setCategoryStatus(categoryStatus);
		pc.setUpdateUserId(userId);
		pc.setUpdateTime(LocalDateTime.now());
		boolean b = productCategoryService.updateById(pc);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:查询车型列表
	 * @param request
	 * @param brandId
	 * @return
	 * JsonResult<List<ProductCategory>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年3月4日 下午7:04:26
	 */
	@RequestMapping(value = "/selectAllList")
    @ResponseBody
    public JsonResult<List<ProductCategory>> selectAllList(HttpServletRequest request,Integer brandId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null) {
			httpCode.setMsg("品牌id不能为空");
			return new JsonResult<List<ProductCategory>>(null, httpCode);
		}
		QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>();
		if(brandId!=null) {			
			queryWrapper.eq("brand_id", brandId);
		}
		List<ProductCategory> list = productCategoryService.list(queryWrapper);
        return new JsonResult<List<ProductCategory>>(list, HttpCode.SUCCESS);
    }
}
